create or replace procedure GetTopWinOdds(i_racedate in varchar2,
                                          i_venue    in varchar2,
                                          i_raceno   in varchar2,
                                          i_endtime  in varchar2, --'yyyy-MM-dd HH:mm:ss'
                                          o_result   out PKG_DATASET.DATASET) is
v_batchno varchar2(20);
begin
    select max(batchno)
    into v_batchno
    from t_winplace_odds w
    where w.racedate = i_racedate and w.venue = i_venue 
          and w.raceno = i_raceno and w.upd_time <= to_date(i_endtime,'yyyy-mm-dd hh24:mi:ss');

    open o_result for
    select 
      w.horseno,
      w.w_odds,
      rpad(w.horseno || '.' || h.horsename,18,' ') || w.w_odds description
    from t_winplace_odds w,t_racehorses h
    where w.racedate = i_racedate
      and w.venue = i_venue
      and w.raceno = i_raceno
      and w.upd_time <= to_date(i_endtime,'yyyy-mm-dd hh24:mi:ss')
      and w.batchno=v_batchno
      and h.racedate = i_racedate
      and h.venue = i_venue
      and h.raceno = i_raceno
      and h.horseno = w.horseno
    order by w.w_odds asc;
end GetTopWinOdds;
/
